package com.hb.common.controller;

import com.hb.common.dto.DbBackupFile;
import com.hb.common.exception.DbBackupException;
import com.hb.common.service.DbBackupFileService;
import com.hb.common.util.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class DbBackupFileController {

    @Resource
    private DbBackupFileService dbBackupFileService;

    /**
     * 根据数据库id，获取备份文件
     */
    @GetMapping("/db-backup/file/{id}")
    public Result<List<DbBackupFile>> getBackupFiles(@PathVariable Integer id) throws DbBackupException {
        return dbBackupFileService.selectBackupFiles(id);
    }

    /**
     * 删除备份文件
     */
    @DeleteMapping("/db-backup/file")
    public Result<Boolean> deleteFile(@RequestParam Integer id, @RequestParam String fileName) {
        return dbBackupFileService.deleteFile(id, fileName);
    }

    /**
     * 下载备份文件
     */
    @GetMapping("/db-backup/file/download")
    public void downloadFile(@RequestParam Integer id,
                             @RequestParam String fileName,
                             HttpServletResponse response) throws DbBackupException {
        dbBackupFileService.downloadFile(id, fileName, response);
    }

    /**
     * 上传数据库程序
     */
    @PostMapping("/db-backup/upload/db-program")
    public Result<Boolean> uploadDbProgram(@RequestParam("file") MultipartFile file) {
        return dbBackupFileService.uploadDbProgram(file);
    }

}
